﻿// Copyright (c) 2008, Colin Burn
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 
// * Neither the name of WTCL nor the names of its contributors may be
// used to endorse or promote products derived from this software
// without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WTCL.Data.Series.Interface;
using WTCL.Data.Type;

namespace WTCL.Data.Series.ValueVsValue
{
    public class MonotonicXValueSeries<TXType, TXSpanType, TYType, TYSpanType> : SeriesBase<ValueData<TXType, TXSpanType, TYType, TYSpanType>>, ISampleXAxisSeries<IDataType<TXType, TXSpanType>>, ISampleYAxisSeries<IDataType<TYType, TYSpanType>>, IMonotonicX<IDataType<TXType, TXSpanType>>
    {
        public int IndexBefore(IDataType<TXType, TXSpanType> value)
        {
            int low = 0;
            int high = this.Samples.Count;
            while (low < high)
            {
                int mid = (low + high) / 2;

                if (this.Samples[mid].X.LessThanOrEqual(value))
                {
                    low = mid + 1;
                }
                else
                {    //can't be high = mid-1: here A[mid] >= value,
                    //so high can't be < mid if A[mid] == value
                    high = mid;
                }
            }

            // high == low, using high or low depends on taste 
            if (low < this.Samples.Count)
            {
                if (!this.Samples[low].X.LessThanOrEqual(value))
                {
                    --low;
                }

                return low; // found
            }
            else
            {
                return -1; // not found
            }
        }

        public int IndexAfter(IDataType<TXType, TXSpanType> value)
        {
            int low = 0;
            int high = this.Samples.Count - 1;
            while (low < high)
            {
                int mid = (low + high) / 2;

                if (this.Samples[mid].X.LessThanOrEqual(value))
                {
                    low = mid + 1;
                }
                else
                {    //can't be high = mid-1: here A[mid] >= value,
                    //so high can't be < mid if A[mid] == value
                    high = mid;
                }
            }

            // high == low, using high or low depends on taste 
            if (low < this.Samples.Count)
            {
                return low; // found
            }
            else
            {
                return -1; // not found
            }
        }

        public IDataType<TXType, TXSpanType> LargestDisplayXValue
        {
            get
            {
                UpdateMinMax();
                return DataTypeMethods<TXType, TXSpanType>.Max(this.actualXMinimum.Abs(), this.actualXMaximum.Abs());
            }
        }

        public IDataType<TYType, TYSpanType> LargestDisplayYValue
        {
            get
            {
                UpdateMinMax();
                return DataTypeMethods<TYType, TYSpanType>.Max(this.actualYMinimum.Abs(), this.actualYMaximum.Abs());
            }
        }

        bool minMaxDirty = true;

        protected override void OnDataChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.minMaxDirty = true;
        }

        private IDataType<TXType, TXSpanType> actualXMinimum;
        private IDataType<TXType, TXSpanType> actualXMaximum;

        public IDataType<TXType, TXSpanType> ActualXMinimum { get { UpdateMinMax(); return this.actualXMinimum; } }
        public IDataType<TXType, TXSpanType> ActualXMaximum { get { UpdateMinMax(); return this.actualXMaximum; } }

        private IDataType<TYType, TYSpanType> actualYMinimum;
        private IDataType<TYType, TYSpanType> actualYMaximum;

        public IDataType<TYType, TYSpanType> ActualYMinimum { get { UpdateMinMax(); return this.actualYMinimum; } }
        public IDataType<TYType, TYSpanType> ActualYMaximum { get { UpdateMinMax(); return this.actualYMaximum; } }

        private IDataType<TXType, TXSpanType> xMinimum;
        private IDataType<TXType, TXSpanType> xMaximum;

        public IDataType<TXType, TXSpanType> XMinimum
        {
            get { return this.xMinimum; }
            set
            {
                this.minMaxDirty = true;
                this.xMinimum = value;
            }
        }

        public IDataType<TXType, TXSpanType> XMaximum
        {
            get { return this.xMaximum; }
            set
            {
                this.minMaxDirty = true;
                this.xMaximum = value;
            }
        }

        private IDataType<TYType, TYSpanType> yMinimum;
        private IDataType<TYType, TYSpanType> yMaximum;

        public IDataType<TYType, TYSpanType> YMinimum
        {
            get { return this.yMinimum; }
            set
            {
                this.minMaxDirty = true;
                this.yMinimum = value;
            }
        }

        public IDataType<TYType, TYSpanType> YMaximum
        {
            get { return this.yMaximum; }
            set
            {
                this.minMaxDirty = true;
                this.yMaximum = value;
            }
        }

        private void UpdateMinMax()
        {
            if (this.minMaxDirty)
            {
                this.actualXMinimum = null;
                this.actualXMaximum = null;

                this.actualYMinimum = null;
                this.actualYMaximum = null;

                if (this.xMinimum != null)
                {
                    this.actualXMinimum = this.xMinimum;
                }

                if (this.xMaximum != null)
                {
                    this.actualXMaximum = this.xMaximum;
                }

                if (this.yMinimum != null)
                {
                    this.actualYMinimum = this.yMinimum;
                }

                if (this.yMaximum != null)
                {
                    this.actualYMaximum = this.yMaximum;
                }

                if (this.xMinimum == null ||
                    this.xMaximum == null ||
                    this.yMinimum == null ||
                    this.yMaximum == null)
                {
                    for (int i = 0; i < this.Samples.Count; ++i)
                    {
                        ValueData<TXType, TXSpanType, TYType, TYSpanType> sample = this.Samples[i];

                        if (this.xMinimum == null)
                        {
                            this.actualXMinimum = DataTypeMethods<TXType, TXSpanType>.Min(sample.X, this.actualXMinimum);
                        }

                        if (this.xMaximum == null)
                        {
                            this.actualXMaximum = DataTypeMethods<TXType, TXSpanType>.Max(sample.X, this.actualXMaximum);
                        }

                        if (this.yMinimum == null)
                        {
                            this.actualYMinimum = DataTypeMethods<TYType, TYSpanType>.Min(sample.Y, this.actualYMinimum);
                        }

                        if (this.yMaximum == null)
                        {
                            this.actualYMaximum = DataTypeMethods<TYType, TYSpanType>.Max(sample.Y, this.actualYMaximum);
                        }
                    }
                }

                this.minMaxDirty = false;
            }
        }
    }
}
